package vip.xiaonuo.facedoor.modular.reactor.listener;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.enums.MemberFeaturesTypeEnum;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;

import javax.annotation.Resource;
import java.util.List;

import static reactor.event.selector.Selectors.$;

/**
 * 梯控监听访客删除事件做人员变更
 */
@Service
@Slf4j
public class FacedoorVisitorDeviceDeleteListener implements Consumer<Event<List<Long>>>, InitializingBean {

    @Autowired
    private Reactor reactor;

    @Autowired
    private VisitorService visitorService;

    @Resource
    private VisitorDeviceMapper visitorDeviceMapper;

    @Override
    public void accept(Event<List<Long>> event) {
        for (Long visitorId : event.getData()) {
            Visitor visitor = visitorService.getById(visitorId);
            if (visitor == null) {
                log.error("访客不存在！");
                continue;
            }
            if (ObjectUtil.equal(visitor.getIssuanceOfVouchers(),1)){
                log.error("未下发凭证 直接清退！");
                continue;
            }

            // 清空人员特征值表格
            MemberFeatures memberFeatures = new MemberFeatures();
            memberFeatures.setDwVisitorId(visitor.getId());
            memberFeatures.setType(MemberFeaturesTypeEnum.VISITOR.getValue());
            memberFeatures.setPlatform(DeviceCategory.SUB_TYPE_FACE);
            reactor.notify(EventTopic.MEMBER_FACE_VALUE_DELETE,Event.wrap(memberFeatures));


            List<VisitorDevice> visitorDevices = visitorDeviceMapper.selectList(new LambdaQueryWrapper<VisitorDevice>()
                    .eq(VisitorDevice::getVisitorId, visitor.getId())
                    .in(
                            VisitorDevice::getStatus,
                            MemberDeviceConstant.ADD_FAIL,
                            MemberDeviceConstant.UPDATE_FAIL,
                            MemberDeviceConstant.DELETE_FAIL,
                            MemberDeviceConstant.SYNC_SUCCESS));
            for (VisitorDevice visitorDevice : visitorDevices) {
                if (ObjectUtil.equal(MemberDeviceConstant.ADD_FAIL, visitorDevice.getStatus())||ObjectUtil.equal(MemberDeviceConstant.ADD, visitorDevice.getStatus())) {
                    visitorDeviceMapper.deleteById(visitorDevice.getId());
                } else {
                    visitorDeviceMapper.updateById(visitorDevice.setStatus(MemberDeviceConstant.DELETE));
                }
            }
        }
    }

    @Override
    public void afterPropertiesSet() {
        reactor.on($(EventTopic.VISITOR_DEVICE_DELETE), this);
    }
}
